package com.supcon.exchangedata.service.lq;

import com.supcon.exchangedata.pojo.entity.oracle.BaseGeoArea;
import com.supcon.exchangedata.pojo.entity.oracle.BaseGeoAreaDetail;
import com.supcon.exchangedata.pojo.entity.oracle.GisPoint;
import com.supcon.exchangedata.pojo.entity.mysql.operation.lq.LqMaintainBase;
import com.supcon.exchangedata.pojo.entity.mysql.operation.lq.LqMaintainFacilityBase;
import com.supcon.exchangedata.pojo.entity.mysql.operation.lq.LqMaintainImportantFacilityBase;
import com.supcon.exchangedata.repository.master.MasterBaseAreaRepository;
import com.supcon.exchangedata.repository.master.MasterBaseGeoAreaDetailRepository;
import com.supcon.exchangedata.repository.master.MasterGisPointRepository;
import com.supcon.exchangedata.repository.slave7.SlaveLqBasicInfo;
import com.supcon.exchangedata.repository.slave7.SlaveLqBasicInfo1;
import com.supcon.exchangedata.repository.slave7.SlaveLqOperationInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: yangbin
 * @Date: 2022/12/21/9:54
 * @Description:
 */
@Service
@Slf4j
public class LqBasicService {
    @Resource
    private MasterBaseAreaRepository masterBaseAreaRepository;
    @Resource
    private MasterGisPointRepository masterGisPointRepository;
    @Resource
    private MasterBaseGeoAreaDetailRepository masterBaseGeoAreaDetailRepository;
    @Resource
    private SlaveLqOperationInfo slaveLqOperationInfo;

    @Resource
    private SlaveLqBasicInfo slaveLqBasicInfo;

    @Resource
    private SlaveLqBasicInfo1 slaveLqBasicInfo1;

    @Transactional
    public void updateLqBasicInfo() {
        //养护设施基础数据 5099
        List<BaseGeoArea> baseGeoAreaByAreaType = masterBaseAreaRepository.findBaseGeoAreaByAreaType(5099L);
        List<LqMaintainFacilityBase> all = slaveLqBasicInfo.findAll();
        if (CollectionUtils.isEmpty(baseGeoAreaByAreaType)) {
            if (!CollectionUtils.isEmpty(all)) {
                List<BaseGeoArea> baseGeoAreaList = new ArrayList<>();
                for (LqMaintainFacilityBase sgjjtStation : all) {
                    BaseGeoArea baseGeoArea = new BaseGeoArea();
                    baseGeoArea.setAreaCode("lqyhss" + sgjjtStation.getTongid());
                    baseGeoArea.setAreaName(sgjjtStation.getFacilityName());
                    baseGeoArea.setAreaType(5099L);
                    //设施类型
                    baseGeoArea.setExtension1(sgjjtStation.getFacilityType());
                    baseGeoArea.setRemarks(sgjjtStation.getDescription());
                    baseGeoArea.setAddress(sgjjtStation.getLocation());
                    //0 基础 1 重要
                    Integer importantFlag = sgjjtStation.getImportantFlag();
                    if (importantFlag == 0) {
                        baseGeoArea.setExtension2("基础");
                    }
                    if (importantFlag == 1) {
                        baseGeoArea.setExtension2("重要");
                    }
                    Boolean flag = sgjjtStation.getFlag();
                    if (!flag) {
                        baseGeoArea.setExtension3("正常");
                    }


                    /* baseGeoArea.setParentId(21856L);*/
                    if (sgjjtStation.getLatitudeStart() != null) {
                        baseGeoArea.setLatitude(sgjjtStation.getLatitudeStart().doubleValue());
                    }
                    if (sgjjtStation.getLongitudeStart() != null) {
                        baseGeoArea.setLongitude(sgjjtStation.getLongitudeStart().doubleValue());
                    }


                    baseGeoArea.setUpdator("jpa-program");
                    baseGeoArea.setUpdateDate(LocalDateTime.now());
                    baseGeoArea.setAddress(sgjjtStation.getLocation());
                    baseGeoArea.setRemarks(sgjjtStation.getDescription());
                    baseGeoAreaList.add(baseGeoArea);
                }
                masterBaseAreaRepository.batchBaseGeoAll(baseGeoAreaList);
            }
        } else {
            if (!CollectionUtils.isEmpty(all)) {
                List<BaseGeoArea> addBaseList = new ArrayList<>();
                for (LqMaintainFacilityBase sgjjtStation : all) {
                    String baseName = sgjjtStation.getFacilityName();
                    boolean update = false;
                    for (BaseGeoArea baseGeoArea : baseGeoAreaByAreaType) {
                        String areaName = baseGeoArea.getAreaName();
                        if (baseName.equals(areaName)) {
                            if (sgjjtStation.getLatitudeStart() != null) {
                                baseGeoArea.setLatitude(sgjjtStation.getLatitudeStart().doubleValue());
                            }
                            if (sgjjtStation.getLongitudeStart() != null) {
                                baseGeoArea.setLongitude(sgjjtStation.getLongitudeStart().doubleValue());
                            }

                            baseGeoArea.setUpdator("jpa-program");
                            baseGeoArea.setUpdateDate(LocalDateTime.now());
                            baseGeoArea.setAddress(sgjjtStation.getLocation());
                            baseGeoArea.setRemarks(sgjjtStation.getDescription());
                            //设施类型
                            baseGeoArea.setExtension1(sgjjtStation.getFacilityType());
                            baseGeoArea.setRemarks(sgjjtStation.getDescription());
                            baseGeoArea.setAddress(sgjjtStation.getLocation());
                            //0 基础 1 重要
                            Integer importantFlag = sgjjtStation.getImportantFlag();
                            if (importantFlag == 0) {
                                baseGeoArea.setExtension2("基础");
                            }
                            if (importantFlag == 1) {
                                baseGeoArea.setExtension2("重要");
                            }
                            Boolean flag = sgjjtStation.getFlag();
                            if (!flag) {
                                baseGeoArea.setExtension3("正常");
                            }
                            addBaseList.add(baseGeoArea);
                            update = true;
                            break;
                        }
                    }
                    if (!update) {
                        BaseGeoArea baseGeoArea = new BaseGeoArea();
                        baseGeoArea.setAreaCode("lqyhss" + sgjjtStation.getTongid());
                        baseGeoArea.setAreaName(sgjjtStation.getFacilityName());
                        baseGeoArea.setAreaType(5099L);
                        /*baseGeoArea.setParentId(21856L);*/
                        if (sgjjtStation.getLatitudeStart() != null) {
                            baseGeoArea.setLatitude(sgjjtStation.getLatitudeStart().doubleValue());
                        }
                        if (sgjjtStation.getLongitudeStart() != null) {
                            baseGeoArea.setLongitude(sgjjtStation.getLongitudeStart().doubleValue());
                        }

                        baseGeoArea.setUpdator("jpa-program");
                        //设施类型
                        baseGeoArea.setExtension1(sgjjtStation.getFacilityType());
                        baseGeoArea.setRemarks(sgjjtStation.getDescription());
                        baseGeoArea.setAddress(sgjjtStation.getLocation());
                        //0 基础 1 重要
                        Integer importantFlag = sgjjtStation.getImportantFlag();
                        if (importantFlag == 0) {
                            baseGeoArea.setExtension2("基础");
                        }
                        if (importantFlag == 1) {
                            baseGeoArea.setExtension2("重要");
                        }
                        Boolean flag = sgjjtStation.getFlag();
                        if (!flag) {
                            baseGeoArea.setExtension3("正常");
                        }
                        baseGeoArea.setUpdateDate(LocalDateTime.now());
                        baseGeoArea.setAddress(sgjjtStation.getLocation());
                        baseGeoArea.setRemarks(sgjjtStation.getDescription());
                        addBaseList.add(baseGeoArea);
                    }
                }
                if (!CollectionUtils.isEmpty(addBaseList)) {
                    masterBaseAreaRepository.saveAll(addBaseList);
                }

            }
        }

    }


    @Transactional
    public void updateLqBasicInfoNew() {
        masterBaseAreaRepository.deleteBaseGeoAreaByAreaType(5099L);
        masterGisPointRepository.deleteGisPointByBusinessCategory("5099");
        //查询出应急库内停保基地  5099
        List<BaseGeoArea> baseGeoAreaByAreaType = masterBaseAreaRepository.findBaseGeoAreaByAreaType(5099L);
        List<LqMaintainFacilityBase> all = slaveLqBasicInfo.findAll();
        if (CollectionUtils.isEmpty(baseGeoAreaByAreaType)) {
            if (!CollectionUtils.isEmpty(all)) {
                List<BaseGeoArea> baseGeoAreaList = new ArrayList<>();
                List<BaseGeoAreaDetail> baseGeoAreaListDetail = new ArrayList<>();
                List<GisPoint> gisPointList = new ArrayList<>();
                for (LqMaintainFacilityBase sgjjtStation : all) {
                    BaseGeoArea baseGeoArea = new BaseGeoArea();
                    baseGeoArea.setAreaCode("lqyhss" + sgjjtStation.getTongid());
                    baseGeoArea.setAreaName(sgjjtStation.getFacilityName());
                    baseGeoArea.setAreaType(5099L);
                    //设施类型
                    baseGeoArea.setExtension1(sgjjtStation.getFacilityType());
                    baseGeoArea.setRemarks(sgjjtStation.getDescription());
                    baseGeoArea.setAddress(sgjjtStation.getLocation());
                    //0 基础 1 重要
                    Integer importantFlag = sgjjtStation.getImportantFlag();
                    if (importantFlag == 0) {
                        baseGeoArea.setExtension2("基础");
                    }
                    if (importantFlag == 1) {
                        baseGeoArea.setExtension2("重要");
                    }
                    Boolean flag = sgjjtStation.getFlag();
                    if (!flag) {
                        baseGeoArea.setExtension3("正常");
                    }
                     baseGeoArea.setParentId(5792518L);
                    if (sgjjtStation.getLatitudeStart() != null) {
                        baseGeoArea.setLatitude(sgjjtStation.getLatitudeStart().doubleValue());
                    }
                    if (sgjjtStation.getLongitudeStart() != null) {
                        baseGeoArea.setLongitude(sgjjtStation.getLongitudeStart().doubleValue());
                    }
                    baseGeoArea.setUpdator("jpa-program");
                    baseGeoArea.setUpdateDate(LocalDateTime.now());
                    baseGeoArea.setAddress(sgjjtStation.getLocation());
                    baseGeoArea.setRemarks(sgjjtStation.getDescription());
                    baseGeoAreaList.add(baseGeoArea);
                }
                masterBaseAreaRepository.batchBaseGeoAll(baseGeoAreaList);

                for (BaseGeoArea baseGeoArea : baseGeoAreaList) {
                    /* BaseGeoAreaDetail baseGeoAreaDetail = baseGeoArea.getBaseGeoAreaDetail();*/
                   /* baseGeoAreaDetail.setAreaId(baseGeoArea.getId());
                    baseGeoAreaListDetail.add(baseGeoAreaDetail);*/
                    GisPoint gisPoint = new GisPoint();
                    gisPoint.setObjId(baseGeoArea.getId());
                    gisPoint.setYwCategory("5099");
                    gisPoint.setObjType(1L);
                    gisPoint.setLongitude(baseGeoArea.getLongitude());
                    gisPoint.setLatitude(baseGeoArea.getLatitude());
                    gisPoint.setName(baseGeoArea.getAreaName());
                    gisPoint.setCode(baseGeoArea.getAreaCode());
                    gisPoint.setBusinessCategory("5099");
                    gisPointList.add(gisPoint);
                }
                masterGisPointRepository.batchGisPointDetailAll(gisPointList);
            }
        } else {
            if (!CollectionUtils.isEmpty(all)) {
                List<BaseGeoArea> addBaseList = new ArrayList<>();
                for (LqMaintainFacilityBase sgjjtStation : all) {
                    String baseName = sgjjtStation.getFacilityName();
                    boolean update = false;
                    for (BaseGeoArea baseGeoArea : baseGeoAreaByAreaType) {
                        String areaName = baseGeoArea.getAreaName();
                        if (baseName.equals(areaName)) {
                            if (sgjjtStation.getLatitudeStart() != null) {
                                baseGeoArea.setLatitude(sgjjtStation.getLatitudeStart().doubleValue());
                            }
                            if (sgjjtStation.getLongitudeStart() != null) {
                                baseGeoArea.setLongitude(sgjjtStation.getLongitudeStart().doubleValue());
                            }
                            baseGeoArea.setUpdator("jpa-program");
                            baseGeoArea.setUpdateDate(LocalDateTime.now());
                            baseGeoArea.setAddress(sgjjtStation.getLocation());
                            baseGeoArea.setRemarks(sgjjtStation.getDescription());
                            //设施类型
                            baseGeoArea.setExtension1(sgjjtStation.getFacilityType());
                            baseGeoArea.setRemarks(sgjjtStation.getDescription());
                            baseGeoArea.setAddress(sgjjtStation.getLocation());
                            //0 基础 1 重要
                            Integer importantFlag = sgjjtStation.getImportantFlag();
                            if (importantFlag == 0) {
                                baseGeoArea.setExtension2("基础");
                            }
                            if (importantFlag == 1) {
                                baseGeoArea.setExtension2("重要");
                            }
                            Boolean flag = sgjjtStation.getFlag();
                            if (!flag) {
                                baseGeoArea.setExtension3("正常");
                            }
                            addBaseList.add(baseGeoArea);
                            update = true;
                            break;
                        }
                    }
                    if (!update) {
                        BaseGeoArea baseGeoArea = new BaseGeoArea();
                        baseGeoArea.setAreaCode("lqyhss" + sgjjtStation.getTongid());
                        baseGeoArea.setAreaName(sgjjtStation.getFacilityName());
                        baseGeoArea.setAreaType(5099L);
                        baseGeoArea.setParentId(5792518L);
                        if (sgjjtStation.getLatitudeStart() != null) {
                            baseGeoArea.setLatitude(sgjjtStation.getLatitudeStart().doubleValue());
                        }
                        if (sgjjtStation.getLongitudeStart() != null) {
                            baseGeoArea.setLongitude(sgjjtStation.getLongitudeStart().doubleValue());
                        }

                        baseGeoArea.setUpdator("jpa-program");
                        //设施类型
                        baseGeoArea.setExtension1(sgjjtStation.getFacilityType());
                        baseGeoArea.setRemarks(sgjjtStation.getDescription());
                        baseGeoArea.setAddress(sgjjtStation.getLocation());
                        //0 基础 1 重要
                        Integer importantFlag = sgjjtStation.getImportantFlag();
                        if (importantFlag == 0) {
                            baseGeoArea.setExtension2("基础");
                        }
                        if (importantFlag == 1) {
                            baseGeoArea.setExtension2("重要");
                        }
                        Boolean flag = sgjjtStation.getFlag();
                        if (!flag) {
                            baseGeoArea.setExtension3("正常");
                        }
                        baseGeoArea.setUpdateDate(LocalDateTime.now());
                        baseGeoArea.setAddress(sgjjtStation.getLocation());
                        baseGeoArea.setRemarks(sgjjtStation.getDescription());
                        addBaseList.add(baseGeoArea);
                    }
                }

                if (!CollectionUtils.isEmpty(addBaseList)) {
                    for (BaseGeoArea baseGeoArea : addBaseList) {
                        BaseGeoArea save = masterBaseAreaRepository.save(baseGeoArea);
                        GisPoint distinctByObjId = masterGisPointRepository.findDistinctByObjId(baseGeoArea.getId());
                        if (distinctByObjId == null) {
                            GisPoint gisPoint = new GisPoint();
                            gisPoint.setObjId(baseGeoArea.getId());
                            gisPoint.setYwCategory("5099");
                            gisPoint.setObjType(1L);
                            gisPoint.setLongitude(baseGeoArea.getLongitude());
                            gisPoint.setLatitude(baseGeoArea.getLatitude());
                            gisPoint.setName(baseGeoArea.getAreaName());
                            gisPoint.setCode(baseGeoArea.getAreaCode());
                            gisPoint.setBusinessCategory("5099");
                            masterGisPointRepository.save(gisPoint);
                        } else {
                            GisPoint gisPoint = distinctByObjId;
                            gisPoint.setObjId(baseGeoArea.getId());
                            gisPoint.setYwCategory("5099");
                            gisPoint.setObjType(1L);
                            gisPoint.setLongitude(baseGeoArea.getLongitude());
                            gisPoint.setLatitude(baseGeoArea.getLatitude());
                            gisPoint.setName(baseGeoArea.getAreaName());
                            gisPoint.setCode(baseGeoArea.getAreaCode());
                            gisPoint.setBusinessCategory("5099");
                            masterGisPointRepository.save(gisPoint);
                        }

                    }
                }

            }

        }
    }


    @Transactional
    public void updateLqBasicInfo1() {
        //重要设施基础数据 5088
        List<BaseGeoArea> baseGeoAreaByAreaType = masterBaseAreaRepository.findBaseGeoAreaByAreaType(5088L);
        List<LqMaintainImportantFacilityBase> all = slaveLqBasicInfo1.findAll();
        if (CollectionUtils.isEmpty(baseGeoAreaByAreaType)) {
            if (!CollectionUtils.isEmpty(all)) {
                List<BaseGeoArea> baseGeoAreaList = new ArrayList<>();
                for (LqMaintainImportantFacilityBase sgjjtStation : all) {
                    BaseGeoArea baseGeoArea = new BaseGeoArea();
                    baseGeoArea.setAreaCode("lqzdyhss" + sgjjtStation.getTongid());
                    baseGeoArea.setAreaName(sgjjtStation.getFacilityName());
                    baseGeoArea.setAreaType(5088L);
                    //设施类型
                    baseGeoArea.setExtension1(sgjjtStation.getFacilityType());
                    baseGeoArea.setRemarks(sgjjtStation.getDescription());
                    baseGeoArea.setAddress(sgjjtStation.getLocation());
                    //0 基础 1 重要
                    baseGeoArea.setExtension2("重要");
                    Boolean flag = sgjjtStation.getFlag();
                    if (!flag) {
                        baseGeoArea.setExtension3("正常");
                    }


                    /* baseGeoArea.setParentId(21856L);*/
                    if (sgjjtStation.getFacilityLatitude() != null) {
                        baseGeoArea.setLatitude(sgjjtStation.getFacilityLatitude().doubleValue());
                    }
                    if (sgjjtStation.getFacilityLongitude() != null) {
                        baseGeoArea.setLongitude(sgjjtStation.getFacilityLongitude().doubleValue());
                    }


                    baseGeoArea.setUpdator("jpa-program");
                    baseGeoArea.setUpdateDate(LocalDateTime.now());
                    baseGeoArea.setAddress(sgjjtStation.getLocation());
                    baseGeoArea.setRemarks(sgjjtStation.getDescription());
                    baseGeoAreaList.add(baseGeoArea);
                }
                masterBaseAreaRepository.batchBaseGeoAll(baseGeoAreaList);
            }
        } else {
            if (!CollectionUtils.isEmpty(all)) {
                List<BaseGeoArea> addBaseList = new ArrayList<>();
                for (LqMaintainImportantFacilityBase sgjjtStation : all) {
                    String baseName = sgjjtStation.getFacilityName();
                    boolean update = false;
                    for (BaseGeoArea baseGeoArea : baseGeoAreaByAreaType) {
                        String areaName = baseGeoArea.getAreaName();
                        if (baseName.equals(areaName)) {
                            if (sgjjtStation.getFacilityLatitude() != null) {
                                baseGeoArea.setLatitude(sgjjtStation.getFacilityLatitude().doubleValue());
                            }
                            if (sgjjtStation.getFacilityLongitude() != null) {
                                baseGeoArea.setLongitude(sgjjtStation.getFacilityLongitude().doubleValue());
                            }

                            baseGeoArea.setUpdator("jpa-program");
                            baseGeoArea.setUpdateDate(LocalDateTime.now());
                            baseGeoArea.setAddress(sgjjtStation.getLocation());
                            baseGeoArea.setRemarks(sgjjtStation.getDescription());
                            //设施类型
                            baseGeoArea.setExtension1(sgjjtStation.getFacilityType());
                            baseGeoArea.setRemarks(sgjjtStation.getDescription());
                            baseGeoArea.setAddress(sgjjtStation.getLocation());
                            baseGeoArea.setExtension2("重要");
                            Boolean flag = sgjjtStation.getFlag();
                            if (!flag) {
                                baseGeoArea.setExtension3("正常");
                            }
                            addBaseList.add(baseGeoArea);
                            update = true;
                            break;
                        }
                    }
                    if (!update) {
                        BaseGeoArea baseGeoArea = new BaseGeoArea();
                        baseGeoArea.setAreaCode("lqyhss" + sgjjtStation.getTongid());
                        baseGeoArea.setAreaName(sgjjtStation.getFacilityName());
                        baseGeoArea.setAreaType(5099L);
                        /*baseGeoArea.setParentId(21856L);*/
                        if (sgjjtStation.getFacilityLatitude() != null) {
                            baseGeoArea.setLatitude(sgjjtStation.getFacilityLongitude().doubleValue());
                        }
                        if (sgjjtStation.getFacilityLongitude() != null) {
                            baseGeoArea.setLongitude(sgjjtStation.getFacilityLongitude().doubleValue());
                        }

                        baseGeoArea.setUpdator("jpa-program");
                        //设施类型
                        baseGeoArea.setExtension1(sgjjtStation.getFacilityType());
                        baseGeoArea.setRemarks(sgjjtStation.getDescription());
                        baseGeoArea.setAddress(sgjjtStation.getLocation());
                        //0 基础 1 重要
                        baseGeoArea.setExtension2("重要");
                        Boolean flag = sgjjtStation.getFlag();
                        if (!flag) {
                            baseGeoArea.setExtension3("正常");
                        }
                        baseGeoArea.setUpdateDate(LocalDateTime.now());
                        baseGeoArea.setAddress(sgjjtStation.getLocation());
                        baseGeoArea.setRemarks(sgjjtStation.getDescription());
                        addBaseList.add(baseGeoArea);
                    }
                }
                if (!CollectionUtils.isEmpty(addBaseList)) {
                    masterBaseAreaRepository.saveAll(addBaseList);
                }

            }
        }

    }


    @Transactional
    public void updateLqBasicInfo1New() {
        masterBaseAreaRepository.deleteBaseGeoAreaByAreaType(5088L);
        masterGisPointRepository.deleteGisPointByBusinessCategory("5088");
        //重要设施基础数据 5088
        List<BaseGeoArea> baseGeoAreaByAreaType = masterBaseAreaRepository.findBaseGeoAreaByAreaType(5088L);
        List<LqMaintainImportantFacilityBase> all = slaveLqBasicInfo1.findAll();
        if (CollectionUtils.isEmpty(baseGeoAreaByAreaType)) {
            if (!CollectionUtils.isEmpty(all)) {
                List<BaseGeoArea> baseGeoAreaList = new ArrayList<>();
                List<BaseGeoAreaDetail> baseGeoAreaListDetail = new ArrayList<>();
                List<GisPoint> gisPointList = new ArrayList<>();
                for (LqMaintainImportantFacilityBase sgjjtStation : all) {
                    BaseGeoArea baseGeoArea = new BaseGeoArea();
                    baseGeoArea.setAreaCode("lqzdyhss" + sgjjtStation.getTongid());
                    baseGeoArea.setAreaName(sgjjtStation.getFacilityName());
                    baseGeoArea.setAreaType(5088L);
                    //设施类型
                    baseGeoArea.setExtension1(sgjjtStation.getFacilityType());
                    baseGeoArea.setRemarks(sgjjtStation.getDescription());
                    baseGeoArea.setAddress(sgjjtStation.getLocation());
                    //0 基础 1 重要
                    baseGeoArea.setExtension2("重要");
                    Boolean flag = sgjjtStation.getFlag();
                    if (!flag) {
                        baseGeoArea.setExtension3("正常");
                    }


                    baseGeoArea.setParentId(5792519L);
                    if (sgjjtStation.getFacilityLatitude() != null) {
                        baseGeoArea.setLatitude(sgjjtStation.getFacilityLatitude().doubleValue());
                    }
                    if (sgjjtStation.getFacilityLongitude() != null) {
                        baseGeoArea.setLongitude(sgjjtStation.getFacilityLongitude().doubleValue());
                    }


                    baseGeoArea.setUpdator("jpa-program");
                    baseGeoArea.setUpdateDate(LocalDateTime.now());
                    baseGeoArea.setAddress(sgjjtStation.getLocation());
                    baseGeoArea.setRemarks(sgjjtStation.getDescription());
                    baseGeoAreaList.add(baseGeoArea);
                }
                masterBaseAreaRepository.batchBaseGeoAll(baseGeoAreaList);

                for (BaseGeoArea baseGeoArea : baseGeoAreaList) {
                    /* BaseGeoAreaDetail baseGeoAreaDetail = baseGeoArea.getBaseGeoAreaDetail();*/
                   /* baseGeoAreaDetail.setAreaId(baseGeoArea.getId());
                    baseGeoAreaListDetail.add(baseGeoAreaDetail);*/
                    GisPoint gisPoint = new GisPoint();
                    gisPoint.setObjId(baseGeoArea.getId());
                    gisPoint.setYwCategory("5088");
                    gisPoint.setObjType(1L);
                    gisPoint.setLongitude(baseGeoArea.getLongitude());
                    gisPoint.setLatitude(baseGeoArea.getLatitude());
                    gisPoint.setName(baseGeoArea.getAreaName());
                    gisPoint.setCode(baseGeoArea.getAreaCode());
                    gisPoint.setBusinessCategory("5088");
                    gisPointList.add(gisPoint);
                }
                masterGisPointRepository.batchGisPointDetailAll(gisPointList);
            }
        } else {
            if (!CollectionUtils.isEmpty(all)) {
                List<BaseGeoArea> addBaseList = new ArrayList<>();
                for (LqMaintainImportantFacilityBase sgjjtStation : all) {
                    String baseName = sgjjtStation.getFacilityName();
                    boolean update = false;
                    for (BaseGeoArea baseGeoArea : baseGeoAreaByAreaType) {
                        String areaName = baseGeoArea.getAreaName();
                        if (baseName.equals(areaName)) {
                            if (sgjjtStation.getFacilityLatitude() != null) {
                                baseGeoArea.setLatitude(sgjjtStation.getFacilityLatitude().doubleValue());
                            }
                            if (sgjjtStation.getFacilityLongitude() != null) {
                                baseGeoArea.setLongitude(sgjjtStation.getFacilityLongitude().doubleValue());
                            }

                            baseGeoArea.setUpdator("jpa-program");
                            baseGeoArea.setUpdateDate(LocalDateTime.now());
                            baseGeoArea.setAddress(sgjjtStation.getLocation());
                            baseGeoArea.setRemarks(sgjjtStation.getDescription());
                            //设施类型
                            baseGeoArea.setExtension1(sgjjtStation.getFacilityType());
                            baseGeoArea.setRemarks(sgjjtStation.getDescription());
                            baseGeoArea.setAddress(sgjjtStation.getLocation());
                            baseGeoArea.setExtension2("重要");
                            Boolean flag = sgjjtStation.getFlag();
                            if (!flag) {
                                baseGeoArea.setExtension3("正常");
                            }
                            addBaseList.add(baseGeoArea);
                            update = true;
                            break;
                        }
                    }
                    if (!update) {
                        BaseGeoArea baseGeoArea = new BaseGeoArea();
                        baseGeoArea.setAreaCode("lqyhss" + sgjjtStation.getTongid());
                        baseGeoArea.setAreaName(sgjjtStation.getFacilityName());
                        baseGeoArea.setAreaType(5099L);
                        baseGeoArea.setParentId(5792519L);
                        if (sgjjtStation.getFacilityLatitude() != null) {
                            baseGeoArea.setLatitude(sgjjtStation.getFacilityLongitude().doubleValue());
                        }
                        if (sgjjtStation.getFacilityLongitude() != null) {
                            baseGeoArea.setLongitude(sgjjtStation.getFacilityLongitude().doubleValue());
                        }

                        baseGeoArea.setUpdator("jpa-program");
                        //设施类型
                        baseGeoArea.setExtension1(sgjjtStation.getFacilityType());
                        baseGeoArea.setRemarks(sgjjtStation.getDescription());
                        baseGeoArea.setAddress(sgjjtStation.getLocation());
                        //0 基础 1 重要
                        baseGeoArea.setExtension2("重要");
                        Boolean flag = sgjjtStation.getFlag();
                        if (!flag) {
                            baseGeoArea.setExtension3("正常");
                        }
                        baseGeoArea.setUpdateDate(LocalDateTime.now());
                        baseGeoArea.setAddress(sgjjtStation.getLocation());
                        baseGeoArea.setRemarks(sgjjtStation.getDescription());
                        addBaseList.add(baseGeoArea);
                    }
                }

                if (!CollectionUtils.isEmpty(addBaseList)) {
                    for (BaseGeoArea baseGeoArea : addBaseList) {
                        BaseGeoArea save = masterBaseAreaRepository.save(baseGeoArea);
                        GisPoint distinctByObjId = masterGisPointRepository.findDistinctByObjId(baseGeoArea.getId());
                        if (distinctByObjId == null) {
                            GisPoint gisPoint = new GisPoint();
                            gisPoint.setObjId(baseGeoArea.getId());
                            gisPoint.setYwCategory("5088");
                            gisPoint.setObjType(1L);
                            gisPoint.setLongitude(baseGeoArea.getLongitude());
                            gisPoint.setLatitude(baseGeoArea.getLatitude());
                            gisPoint.setName(baseGeoArea.getAreaName());
                            gisPoint.setCode(baseGeoArea.getAreaCode());
                            gisPoint.setBusinessCategory("5088");
                            masterGisPointRepository.save(gisPoint);
                        } else {
                            GisPoint gisPoint = distinctByObjId;
                            gisPoint.setObjId(baseGeoArea.getId());
                            gisPoint.setYwCategory("5088");
                            gisPoint.setObjType(1L);
                            gisPoint.setLongitude(baseGeoArea.getLongitude());
                            gisPoint.setLatitude(baseGeoArea.getLatitude());
                            gisPoint.setName(baseGeoArea.getAreaName());
                            gisPoint.setCode(baseGeoArea.getAreaCode());
                            gisPoint.setBusinessCategory("5088");
                            masterGisPointRepository.save(gisPoint);
                        }

                    }
                }

            }

        }
    }

    @Transactional
    public void updateLqBasic() {
        masterBaseAreaRepository.deleteBaseGeoAreaByAreaType(5017L);
        masterGisPointRepository.deleteGisPointByBusinessCategory("5017");
        //查询出应急库内停保基地  5017   21856
        List<BaseGeoArea> baseGeoAreaByAreaType = masterBaseAreaRepository.findBaseGeoAreaByAreaType(5017L);
        List<LqMaintainBase> all = slaveLqOperationInfo.findAll();
        if (CollectionUtils.isEmpty(baseGeoAreaByAreaType)) {
            if (!CollectionUtils.isEmpty(all)) {
                List<BaseGeoArea> baseGeoAreaList = new ArrayList<>();
                List<BaseGeoAreaDetail> baseGeoAreaListDetail = new ArrayList<>();
                List<GisPoint> gisPointList = new ArrayList<>();
                for (LqMaintainBase sgjjtStation : all) {
                    BaseGeoArea baseGeoArea = new BaseGeoArea();
                    baseGeoArea.setAreaCode("lqyhjd" + sgjjtStation.getTongid());
                    baseGeoArea.setAreaName(sgjjtStation.getBaseName());
                    baseGeoArea.setAreaType(5017L);
                    baseGeoArea.setParentId(21856L);
                    baseGeoArea.setLongitude(sgjjtStation.getLongitude().doubleValue());
                    baseGeoArea.setLatitude(sgjjtStation.getLatitude().doubleValue());
                    baseGeoArea.setUpdator("jpa-program");
                    baseGeoArea.setUpdateDate(LocalDateTime.now());
                    baseGeoArea.setAddress(sgjjtStation.getLocation());
                    baseGeoArea.setRemarks(sgjjtStation.getDescription());
                    baseGeoAreaList.add(baseGeoArea);
                }
                masterBaseAreaRepository.batchBaseGeoAll(baseGeoAreaList);

                for (BaseGeoArea baseGeoArea : baseGeoAreaList) {
                    /* BaseGeoAreaDetail baseGeoAreaDetail = baseGeoArea.getBaseGeoAreaDetail();*/
                   /* baseGeoAreaDetail.setAreaId(baseGeoArea.getId());
                    baseGeoAreaListDetail.add(baseGeoAreaDetail);*/
                    GisPoint gisPoint = new GisPoint();
                    gisPoint.setObjId(baseGeoArea.getId());
                    gisPoint.setYwCategory("5017");
                    gisPoint.setObjType(1L);
                    gisPoint.setLongitude(baseGeoArea.getLongitude());
                    gisPoint.setLatitude(baseGeoArea.getLatitude());
                    gisPoint.setName(baseGeoArea.getAreaName());
                    gisPoint.setCode(baseGeoArea.getAreaCode());
                    gisPoint.setBusinessCategory("5017");
                    gisPointList.add(gisPoint);
                }
                masterGisPointRepository.batchGisPointDetailAll(gisPointList);
            }
        } else {
            if (!CollectionUtils.isEmpty(all)) {
                List<BaseGeoArea> addBaseList = new ArrayList<>();
                for (LqMaintainBase sgjjtStation : all) {
                    String baseName = sgjjtStation.getBaseName();
                    boolean update = false;
                    for (BaseGeoArea baseGeoArea : baseGeoAreaByAreaType) {
                        String areaName = baseGeoArea.getAreaName();
                        if (baseName.equals(areaName)) {
                            baseGeoArea.setLongitude(sgjjtStation.getLongitude().doubleValue());
                            baseGeoArea.setLatitude(sgjjtStation.getLatitude().doubleValue());
                            baseGeoArea.setUpdator("jpa-program");
                            baseGeoArea.setUpdateDate(LocalDateTime.now());
                            baseGeoArea.setAddress(sgjjtStation.getLocation());
                            baseGeoArea.setRemarks(sgjjtStation.getDescription());
                            addBaseList.add(baseGeoArea);
                            update = true;
                            break;
                        }
                    }
                    if (!update) {

                        BaseGeoArea baseGeoArea = new BaseGeoArea();
                        baseGeoArea.setAreaCode("lqyhjd" + sgjjtStation.getTongid());
                        baseGeoArea.setAreaName(sgjjtStation.getBaseName());
                        baseGeoArea.setAreaType(5017L);
                        baseGeoArea.setParentId(21856L);
                        baseGeoArea.setLongitude(sgjjtStation.getLongitude().doubleValue());
                        baseGeoArea.setLatitude(sgjjtStation.getLatitude().doubleValue());
                        baseGeoArea.setUpdator("jpa-program");
                        baseGeoArea.setUpdateDate(LocalDateTime.now());
                        baseGeoArea.setAddress(sgjjtStation.getLocation());
                        baseGeoArea.setRemarks(sgjjtStation.getDescription());
                        addBaseList.add(baseGeoArea);
                    }
                }

                if (!CollectionUtils.isEmpty(addBaseList)) {
                    for (BaseGeoArea baseGeoArea : addBaseList) {
                        BaseGeoArea save = masterBaseAreaRepository.save(baseGeoArea);
                        GisPoint distinctByObjId = masterGisPointRepository.findDistinctByObjId(baseGeoArea.getId());
                        if (distinctByObjId == null) {
                            GisPoint gisPoint = new GisPoint();
                            gisPoint.setObjId(baseGeoArea.getId());
                            gisPoint.setYwCategory("5017");
                            gisPoint.setObjType(1L);
                            gisPoint.setLongitude(baseGeoArea.getLongitude());
                            gisPoint.setLatitude(baseGeoArea.getLatitude());
                            gisPoint.setName(baseGeoArea.getAreaName());
                            gisPoint.setCode(baseGeoArea.getAreaCode());
                            gisPoint.setBusinessCategory("5017");
                            masterGisPointRepository.save(gisPoint);
                        } else {
                            GisPoint gisPoint = distinctByObjId;
                            gisPoint.setObjId(baseGeoArea.getId());
                            gisPoint.setYwCategory("5017");
                            gisPoint.setObjType(1L);
                            gisPoint.setLongitude(baseGeoArea.getLongitude());
                            gisPoint.setLatitude(baseGeoArea.getLatitude());
                            gisPoint.setName(baseGeoArea.getAreaName());
                            gisPoint.setCode(baseGeoArea.getAreaCode());
                            gisPoint.setBusinessCategory("5017");
                            masterGisPointRepository.save(gisPoint);
                        }

                    }
                }

            }

        }
    }


}
